<?php

class SearchController extends MyController
{
	function actionDichvu($alias = "")
	{
		$this->session["menu_alias"] = $alias;
		
		$dichvu = dichvu::model()->find(array(
			"condition" => "alias like '%".$alias."%'"
		));
		
		$rows = product::model()->findAll(array(
			"condition" => "enable = 1 AND dichvu_id = " . $dichvu->id
		));
		
		$this->data["rows"] = $rows;
		$this->render("dichvu", $this->data);
		
	}
	
	function actionDm($alias = "", $filter = "")
	{
		$this->session["menu_alias"] = $alias;
		
		$dichvu = dichvu::model()->find(array(
			"condition" => "alias like '%".$alias."%'"
		));
		
		$pcategory = pcategory::model()->find(array(
			"condition" => "alias like '%".$filter."%'",
		));
		
		$rows = product::model()->findAll(array(
			"condition" => "enable = 1 AND trash = 0 
				AND dichvu_id = " . $dichvu->id . " AND pcategory_id = " . $pcategory->id
		));
		
		$this->data["rows"] = $rows;
		$this->render("dichvu", $this->data);
		
	}
	
	function actionQuan($alias = "", $filter = "")
	{
		$this->session["menu_alias"] = $alias;
		
		$dichvu = dichvu::model()->find(array(
			"condition" => "alias like '%".$alias."%'"
		));
		
		$quan = quan::model()->find(array(
			"condition" => "alias like '%".$filter."%'",
		));
		
		$rows = product::model()->findAll(array(
			"condition" => "enable = 1 AND trash = 0 
				AND dichvu_id = " . $dichvu->id . " AND quan_id = " . $quan->id
		));
		
		$this->data["rows"] = $rows;
		$this->render("dichvu", $this->data);
		
	}
	
	function actionGia($alias = "", $filter = "")
	{
		$this->session["menu_alias"] = $alias;
		
		$dichvu = dichvu::model()->find(array(
			"condition" => "alias like '%".$alias."%'"
		));
		
		$gia = "";
		
		if (strcmp($filter, "1trieu-3trieu"))
			$gia = "price_number between 1000000 AND 3000000";
		else if (strcmp($filter, "3trieu-5trieu"))
			$gia = "price_number between 3000000 AND 5000000";
		else if (strcmp($filter, "5trieu-10trieu"))
			$gia = "price_number between 5000000 AND 10000000";
		else if (strcmp($filter, "10trieu-15trieu"))
			$gia = "price_number between 10000000 AND 15000000";
		else if (strcmp($filter, "15trieu-20trieu"))
			$gia = "price_number between 15000000 AND 20000000";
		else if (strcmp($filter, "20trieu-50trieu"))
			$gia = "price_number between 20000000 AND 50000000";
		else if (strcmp($filter, "50trieu-100trieu"))
			$gia = "price_number between 50000000 AND 100000000";
		else
			$gia = "price_number >= 100000000";

		$rows = product::model()->findAll(array(
			"condition" => "enable = 1 AND trash = 0 
				AND dichvu_id = " . $dichvu->id . " AND ( " . $gia . " )"
		));
		
		$this->data["rows"] = $rows;
		$this->render("dichvu", $this->data);
		
	}
	
	function actionDientich($alias = "", $filter = "")
	{
		$this->session["menu_alias"] = $alias;
		
		$dichvu = dichvu::model()->find(array(
			"condition" => "alias like '%".$alias."%'"
		));
		
		
		$dientich = "";
		
		if (strcmp($filter, "15m-30m"))
			$dientich = "dien_tich between 15 AND 30";
		else if (strcmp($filter, "30m-50m"))
			$dientich = "dien_tich between 30 AND 50";
		else if (strcmp($filter, "50m-80m"))
			$dientich = "dien_tich between 50 AND 80";
		else if (strcmp($filter, "80m-150m"))
			$dientich = "dien_tich between 80 AND 150";
		else if (strcmp($filter, "150m-200m"))
			$dientich = "dien_tich between 150 AND 200";
		else if (strcmp($filter, "200m-300m"))
			$dientich = "dien_tich between 200 AND 300";
		else if (strcmp($filter, "300m-500m"))
			$dientich = "dien_tich between 300 AND 500";
		else
			$dientich = "dien_tich >= 500";

		$rows = product::model()->findAll(array(
			"condition" => "enable = 1 AND trash = 0 
				AND dichvu_id = " . $dichvu->id . " AND ( " . $dientich . " )"
		));
		
		$this->data["rows"] = $rows;
		$this->render("dichvu", $this->data);
		
	}
	
	function actionAdvancedsearch($dichvu_id, $pcategory_id, $quan_id, $ngay_dang, $gia_1, $gia_2)
	{
		$condition = "enable = 1 AND trash = 0";
		
		
		if ($dichvu_id != 0)
		{
			$condition .= " AND dichvu_id = " . $dichvu_id;
			$dichvu = dichvu::model()->findByPk($dichvu_id);
			$this->session["menu_alias"] = $dichvu->alias;
		}
		
		if ($pcategory_id != 0)
			$condition .= " AND pcategory_id = " . $pcategory_id;
		
		if ($quan_id != 0)
			$condition .= " AND quan_id = " . $quan_id;
		
		if ($gia_1 != 0 && $gia_2 != 0 && $gia_1 <= $gia_2)
			$condition .= " AND (price BETWEEN $gia_1 AND $gia_2)";
		
		$rows = product::model()->findAll(array(
			"condition" => $condition
		));
		
		$this->data["rows"] = $rows;
		$this->render("dichvu", $this->data);
		
	}
}